#substation/equipment, SubstationEquipmentOverCurrentRealyType, types are not showing proper strings
#RegulatorControllerSerialNumber field is not saving
#


#    Copyright 2010 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.
#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import application, cis, command, compatibleunit, geography, oms, staker, unsorted
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
import _mysql as mysql

class Substation(models.Model):
    Name=models.CharField(max_length=250, unique=True)
    Description=models.TextField(null=True, blank=True)
    Assembly=models.ForeignKey('Assembly', null=True, blank=True)
    ActiveIndicator=models.NullBooleanField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substation'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=Substation.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        StringList=[]
        if(not self.ActiveIndicator):
            StringList.append('Inactive ')
        StringList.append(self.Name)
        return ''.join(StringList)

class SubstationEquipmentSetting(models.Model):
    """SubstationEquipmentSetting stores possible settings for various pieces of substation equipment, 
       most commonly '1','2','3','4','5' or 'A', 'B', 'C', 'D', 'E'"""
    Name=models.TextField(unique=True)
    Description=models.TextField()
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substationequipmentsetting'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentSetting.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        return self.Name


class SubstationEquipmentSettingAssignment(models.Model):
    """SubstationEquipmentSettingAssignment stores a history of what settings pieces of substation equipment were 
       set to.  Examples include TransformerDeenergizeTapChangePosition and possibly regulator settings"""
    SubstationEquipment=models.ForeignKey('SubstationEquipment')
    SubstationEquipmentSetting=models.ForeignKey('SubstationEquipmentSetting')
    DateTime=models.DateTimeField()  #DateTime that the equipment setting was set 
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substationequipmentsettingassignment'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentSettingAssignment.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()



class SubstationEquipmentModelType(models.Model):
    """Holds models for various pieces of substation equipment, along with manufacturer names and related info"""
    Name=models.CharField(max_length=250, unique=True)
    Description=models.TextField(null=True, blank=True)
    Manufacturer=models.ForeignKey('Company', related_name='Manufacturer', blank=True, null=True)
    TransformerIndicator=models.NullBooleanField(null=True, blank=True)
    RegulatorIndicator=models.NullBooleanField(null=True, blank=True)
    RegulatorControllerIndicator=models.NullBooleanField(null=True, blank=True)
    TransmissionOverCurrentRelayIndicator=models.NullBooleanField(null=True, blank=True)
    RecloserIndicator=models.NullBooleanField(null=True, blank=True)
    RecloserControllerIndicator=models.NullBooleanField(null=True, blank=True)
    CircuitSwitcherIndicator=models.NullBooleanField(null=True, blank=True)
    BatteryBankIndicator=models.NullBooleanField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationequipmentmodeltype'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentModelType.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        StringList=[]
        if(self.RegulatorIndicator):
            StringList.append('Regulator ')
        if(self.RegulatorControllerIndicator):
            StringList.append('Regulator Controller ')
        if(self.TransmissionOverCurrentRelayIndicator):
            StringList.append('Transmission Over Current Relay ')
        if(self.RecloserIndicator):
            StringList.append('Recloser ')
        if(self.RecloserControllerIndicator):
            StringList.append('Recloser Controller')
        if(self.CircuitSwitcherIndicator):
            StringList.append('Circuit Switcher ')
        if(self.BatteryBankIndicator):
            StringList.append('Battery Bank ')
        StringList.append(self.Name)
        return ''.join(StringList)





class SubstationEquipmentSubstationAssignment(models.Model):
    Substation=models.ForeignKey('Substation')
    SubstationEquipment=models.ForeignKey('SubstationEquipment')
    DateTime=models.DateTimeField()
    Description=models.TextField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationequipmentsubstationassignment'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentSubstationAssignment.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()



class SubstationCircuit(models.Model):
    """SubstationCircuit stores circuit names, along with what substation they are at and the circuit number as used by the GIS"""
    Name=models.TextField(db_column='name', unique=True)
    CircuitNumber=models.IntegerField(db_column='circuitnumber', unique=True)
    Substation=models.ForeignKey('Substation', db_column='substation_id', null=True, blank=True)
    Description=models.TextField(db_column='description')
    AMRCircuitName=models.TextField(db_column='amrcircuitname', null=True, blank=True)
    AMRSubstationIdentifier=models.IntegerField(db_column='amrsubstationidentifier', null=True, blank=True)
    AMRBusIdentifier=models.IntegerField(db_column='amrbusidentifier', null=True, blank=True)
    AMRFeederIdentifier=models.IntegerField(db_column='amrfeederidentifier', null=True, blank=True)

    ResidesOnServerIndicator=models.NullBooleanField(db_column='residesonserverindicator')
    class Meta:
        db_table=u'substationcircuit'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationCircuit.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        return self.Name + ' ' + str(self.CircuitNumber)

class SubstationEquipmentCircuitAssignment(models.Model):
    SubstationCircuit=models.ForeignKey('SubstationCircuit')
    SubstationEquipment=models.ForeignKey('SubstationEquipment')
    DateTime=models.DateTimeField()
    Description=models.TextField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationequipmentcircuitassignment'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentCircuitAssignment.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        return str(self.SubstationCircuit)


class SubstationEquipmentTest(models.Model):
    SubstationEquipment=models.ForeignKey('SubstationEquipment')
    PassIndicator=models.NullBooleanField(null=True, blank=True)
    Description=models.TextField(null=True, blank=True)
    DateTime=models.DateTimeField()
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substationequipmenttest'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentTest.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()

class SubstationEquipment(models.Model):
    """All fields for which an Order is True will be displayed in the specified order"""
    Name=models.TextField(db_column='name', null=True, blank=True)
    SubstationEquipmentModelType=models.ForeignKey('SubstationEquipmentModelType', related_name='SubstationEquipmentModelType', null=True, blank=True)
    CurrentSubstation=models.ForeignKey('Substation', null=True, blank=True)
    InventoryNumber=models.CharField(max_length=250, unique=True, null=True, blank=True)
    CatalogNumber=models.CharField(max_length=250, null=True, blank=True)
    RatedTransmissionVoltage1=models.ForeignKey('Voltage', related_name='SubstationEquipmentRatedTransmissionVoltage1', null=True, blank=True)
    RatedTransmissionVoltage2=models.ForeignKey('Voltage', related_name='SubstationEquipmentRatedTransmissionVoltage2', null=True, blank=True)
    RatedDistributionVoltage1=models.ForeignKey('Voltage', related_name='SubstationEquipmentRatedDistributionVoltage1', null=True, blank=True)
    RatedDistributionVoltage2=models.ForeignKey('Voltage', related_name='SubstationEquipmentRatedDistributionVoltage2', null=True, blank=True)
    OperatorControlVoltage=models.ForeignKey('Voltage', related_name='SubstationEquipmentOperatorControlVoltage', null=True, blank=True)
    CurrentTransmissionVoltage=models.ForeignKey('Voltage', related_name='SubstationEquipmentCurrentTransmissionVoltage', null=True, blank=True)
    CurrentDistributionVoltage=models.ForeignKey('Voltage', related_name='SubstationEquipmentCurrentDistributionVoltage', null=True, blank=True)
    KVARating=models.FloatField(null=True, blank=True)
    MVARating=models.FloatField(null=True, blank=True)
    Manufacturer=models.ForeignKey('Company', blank=True, null=True)
    DateOfManufacture=models.DateField(null=True, blank=True)
    Impedance=models.FloatField(null=True, blank=True)
    OilCapacityGallon=models.FloatField(null=True, blank=True)
    WeightPound=models.FloatField(null=True, blank=True)
    BatteryChargerManufacturer=models.ForeignKey('Company', related_name='BatteryChargerManufacturer', blank=True, null=True)
    BatteryChargerDateOfManufacture=models.DateField(null=True, blank=True)
    BatteryChargerInputVoltage=models.ForeignKey('Voltage', related_name='SubstationEquipmentBatteryChargerInputVoltage', null=True, blank=True)
    BatteryChargerOutputVoltage=models.ForeignKey('Voltage', related_name='SubstationEquipmentBatteryChargerOutputVoltage', null=True, blank=True)
    BatteryChargerACAmps=models.FloatField(null=True, blank=True)
    BatteryChargerDCAmps=models.FloatField(null=True, blank=True)
    BatteryChargerNumberOfCells=models.IntegerField(null=True, blank=True)
    BatteryChargerTypeOfCells=models.TextField(null=True, blank=True)
    BatteryBankBatteryVoltage=models.FloatField(null=True, blank=True)
    BatteryBankBatteryAmpHours=models.FloatField(null=True, blank=True)
    BatteryBankBatteryReserveCapacity=models.FloatField(null=True, blank=True)
    BatteryBankBatteryModelNumber=models.TextField(null=True, blank=True)
    SubstationEquipmentOverCurrentRelayType=models.ForeignKey('SubstationEquipmentModelType', related_name='SubstationEquipmentOverCurrentRelayType', null=True, blank=True)
    SubstationEquipmentDifferentialRelayType=models.ForeignKey('SubstationEquipmentModelType', related_name='SubstationEquipmentDifferentialRealyType', null=True, blank=True)
    SubstationEquipmentRegulatorControllerType=models.ForeignKey('SubstationEquipmentModelType', related_name='SubstationEquipmentRegulatorControllerType', null=True, blank=True)
    RegulatorControllerSerialNumber=models.TextField(null=True, blank=True)
    RecloserControllerType=models.ForeignKey('SubstationEquipmentModelType', related_name='SubstationEquipmentRecloserControllerType', null=True, blank=True)
    SerialNumber=models.CharField(max_length=250)
    Description=models.TextField(null=True, blank=True)
    Assembly=models.ForeignKey('Assembly', null=True, blank=True)
    ActiveIndicator=models.NullBooleanField(null=True, blank=True)
    SubstationEquipmentTemplate=models.ForeignKey('SubstationEquipmentTemplate', null=True, blank=True)
    FanQuantity=models.IntegerField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationequipment'
        app_label='djangoproject'
    def __unicode__(self):
        StringList=[]
        Circuit=self.GetSubstationCircuitAssignment()
        if(Circuit):
            StringList.append(str(Circuit) + ' ')
        Substation=self.CurrentSubstation
        if(Substation):
            StringList.append(str(Substation) + ' ')
        StringList.append(self.InventoryNumber + ' ')
        if(self.SubstationEquipmentModelType):
            StringList.append(str(self.SubstationEquipmentModelType))
        return ''.join(StringList)
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipment.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def GetSubstation(self):
        Assignments=SubstationEquipmentSubstationAssignment.objects.filter(SubstationEquipment=self).order_by('-DateTime')
        if(Assignments.count()):
            return Assignments[0].Substation
        else:
            return None
    def GetSubstationEquipmentSettingAssignment(self):
        Assignments=SubstationEquipmentSettingAssignment.objects.filter(SubstationEquipment=self).order_by('-DateTime')
        if(Assignments.count()):
            return Assignments[0].SubstationEquipmentSetting
        else:
            return None
    def GetSubstationCircuitAssignment(self):
        Assignments=SubstationEquipmentCircuitAssignment.objects.filter(SubstationEquipment=self).order_by('-DateTime')
        if(Assignments.count()):
            return Assignments[0].SubstationCircuit
        else:
            return None
    def SetCurrentSubstation(self):
        self.CurrentSubstation=self.GetSubstation()
        self.save()



class SubstationEquipmentInspection(models.Model):
    """SubstationEquipmentInspection holds the details of each inspection for each piece of substation equipment"""
    SubstationEquipment=models.ForeignKey('SubstationEquipment')
    Description=models.TextField(null=True, blank=True)
    CreateDateTime=models.DateTimeField()
    CreateUserName=models.TextField(null=True, blank=True)
    LastModifyDateTime=models.DateTimeField()
    LastModifyUserName=models.TextField(null=True, blank=True)
    BatteryTest=models.ForeignKey('SubstationInspectionChoice', related_name='BatteryTest', null=True, blank=True)
    BatteryCharge=models.FloatField(null=True, blank=True)
    BatteryVisual=models.ForeignKey('SubstationInspectionChoice', related_name='BatteryVisual', null=True, blank=True)
    BatteryVoltageNormal=models.FloatField(null=True, blank=True)
    Ground=models.ForeignKey('SubstationInspectionChoice', related_name='Ground', null=True, blank=True)
    Visual=models.ForeignKey('SubstationInspectionChoice', related_name='Visual', null=True, blank=True)
    BatteryVoltageTest=models.FloatField(null=True, blank=True)
    MeterKWH=models.FloatField(null=True, blank=True)
    MeterKW=models.FloatField(null=True, blank=True)
    MeterVisual=models.ForeignKey('SubstationInspectionChoice', related_name='MeterVisual', null=True, blank=True)
    RecloserAmperageA=models.FloatField(null=True, blank=True)
    RecloserAmperageB=models.FloatField(null=True, blank=True)
    RecloserAmperageC=models.FloatField(null=True, blank=True)
    RecloserAmperageGround=models.FloatField(null=True, blank=True)
    RecloserMaxDemandA=models.FloatField(null=True, blank=True)
    RecloserMaxDemandB=models.FloatField(null=True, blank=True)
    RecloserMaxDemandC=models.FloatField(null=True, blank=True)
    DemandAmpA=models.FloatField(null=True, blank=True)
    DemandAmpB=models.FloatField(null=True, blank=True)
    DemandAmpC=models.FloatField(null=True, blank=True)
    OverCurrentTrip=models.FloatField(null=True, blank=True)
    BreakerCounter=models.IntegerField(null=True, blank=True)
    RecloserMaxDemandGround=models.FloatField(null=True, blank=True)
    RecloserOperationSummaryA=models.IntegerField(null=True, blank=True)
    RecloserOperationSummaryB=models.IntegerField(null=True, blank=True)
    RecloserOperationSummaryC=models.IntegerField(null=True, blank=True)
    RecloserTargetA=models.FloatField(null=True, blank=True)
    RecloserTargetB=models.FloatField(null=True, blank=True)
    RecloserTargetC=models.FloatField(null=True, blank=True)
    RecloserTargetGround=models.FloatField(null=True, blank=True)
    RegulatorDragHandPositionMinimum=models.FloatField(null=True, blank=True)
    RegulatorDragHandPositionCurrent=models.FloatField(null=True, blank=True)
    RegulatorDragHandPositionMaximum=models.FloatField(null=True, blank=True)
    RegulatorVoltHigh=models.FloatField(null=True, blank=True)
    RegulatorVoltLow=models.FloatField(null=True, blank=True)
    RegulatorVoltCurrent=models.FloatField(null=True, blank=True)
    RegulatorMaxAmp=models.FloatField(null=True, blank=True)
    OperationCounter=models.IntegerField(null=True, blank=True)
    SurgeProtectorVisual=models.ForeignKey('SubstationInspectionChoice', related_name='SubstationEquipmentInspectionSurgeProtectorVisual', null=True, blank=True)
    TransformerOilLeak=models.ForeignKey('SubstationInspectionChoice', related_name='TransformerOilLeak', null=True, blank=True)
    TransformerOilLevel=models.ForeignKey('SubstationInspectionChoice', related_name='TransformerOilLevel', null=True, blank=True)
    TransformerOilTemperatureCurrent=models.FloatField(null=True, blank=True)
    TransformerOilTemperatureMaximum=models.FloatField(null=True, blank=True)
    TransformerNitrogenPressure=models.FloatField(null=True, blank=True)
    TransformerOilVisual=models.ForeignKey('SubstationInspectionChoice', related_name='TransformerOilVisual', null=True, blank=True)
    TransformerWindingTemperatureCurrent=models.FloatField()
    TransformerWindingTemperatureMaximum=models.FloatField()
    MechanismCharge=models.ForeignKey('SubstationInspectionChoice', related_name='MechanismCharge', null=True, blank=True)
    FanWorkingVisual=models.ForeignKey('SubstationInspectionChoice', related_name='FanWorkingVisual', null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationequipmentinspection'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentInspection.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        return str(self.SubstationEquipment) + ' ' + str(self.id) + ' ' + str(self.CreateDateTime)[:10] 

class SubstationEquipmentTemplate(models.Model):
    """SubstationEquipmentTemplate describes exactly what fields should be displayed for a piece of equipment.  The specific
       type of an individual piece of equipment is determined based on the substationequipmenttemplate.
       An Order of 0 means that the field is unimportant/do not display it."""
    Name=models.CharField(max_length=250, unique=True)
    DescriptionOrder=models.TextField(null=True, blank=True)
    BatteryTestOrder=models.FloatField()
    BatteryVoltageNormalOrder=models.FloatField()
    BatteryVoltageTestOrder=models.FloatField()
    BatteryChargeOrder=models.FloatField()
    BatteryVisualOrder=models.FloatField()
    GroundOrder=models.FloatField()
    VisualOrder=models.FloatField()
    MeterKWHOrder=models.FloatField()
    MeterKWOrder=models.FloatField()
    RecloserAmperageAOrder=models.FloatField()
    RecloserAmperageBOrder=models.FloatField()
    RecloserAmperageCOrder=models.FloatField()
    RecloserAmperageGroundOrder=models.FloatField()
    RecloserMaxDemandAOrder=models.FloatField()
    RecloserMaxDemandBOrder=models.FloatField()
    RecloserMaxDemandCOrder=models.FloatField()
    RecloserMaxDemandGroundOrder=models.FloatField()
    RecloserOperationCounterOrder=models.FloatField()
    RecloserOperationSummaryAOrder=models.FloatField()
    RecloserOperationSummaryBOrder=models.FloatField()
    RecloserOperationSummaryCOrder=models.FloatField()
    RecloserTargetAOrder=models.FloatField()
    RecloserTargetBOrder=models.FloatField()
    RecloserTargetCOrder=models.FloatField()
    RecloserTargetGroundOrder=models.FloatField()
    DemandAmpAOrder=models.FloatField()
    DemandAmpBOrder=models.FloatField()
    DemandAmpCOrder=models.FloatField()
    OverCurrentTripOrder=models.FloatField()
    BreakerCounterOrder=models.FloatField()
    RegulatorDragHandPositionMinimumOrder=models.FloatField()
    RegulatorDragHandPositionCurrentOrder=models.FloatField()
    RegulatorDragHandPositionMaximumOrder=models.FloatField()
    RegulatorVoltCurrentOrder=models.FloatField()
    RegulatorVoltLowOrder=models.FloatField()
    RegulatorVoltHighOrder=models.FloatField()
    RegulatorMaxAmpOrder=models.FloatField()
    OperationCounterOrder=models.FloatField()
    SurgeProtectorVisualOrder=models.FloatField()
    TransformerOilLeakOrder=models.FloatField()
    TransformerOilLevelOrder=models.FloatField()
    TransformerOilTemperatureCurrentOrder=models.FloatField()
    TransformerOilTemperatureMaximumOrder=models.FloatField()
    TransformerNitrogenPressureOrder=models.FloatField()
    TransformerOilVisualOrder=models.FloatField()
    TransformerWindingTemperatureCurrentOrder=models.FloatField()
    TransformerWindingTemperatureMaximumOrder=models.FloatField()
    MechanismChargeOrder=models.FloatField()
    FanWorkingVisualOrder=models.FloatField()
    ActiveIndicator=models.NullBooleanField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substationequipmenttemplate'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationEquipmentTemplate.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        StringList=[]
        if(not self.ActiveIndicator):
            StringList.append('Inactive ')
        StringList.append(self.Name)
        return ''.join(StringList)

class SubstationInspection(models.Model):
    Substation=models.ForeignKey('Substation')
    Description=models.TextField(null=True, blank=True)
    CreateDateTime=models.DateTimeField()
    CreateUserName=models.TextField(null=True, blank=True)
    LastModifyDateTime=models.DateTimeField() 
    LastModifyUserName=models.TextField(null=True, blank=True)
    DynasphereVisual=models.ForeignKey('SubstationInspectionChoice', related_name='SubstationInspectionDynasphereVisual', null=True, blank=True)
    BatteryCabinetVisual=models.ForeignKey('SubstationInspectionChoice', related_name='SubstationInspectionBatteryCabinetVisual', null=True, blank=True)
    BrokenPorcelain=models.ForeignKey('SubstationInspectionChoice', related_name='BrokenPorcelain', null=True, blank=True)
    CircuitIdentifier=models.ForeignKey('SubstationInspectionChoice', related_name='CircuitIdentifier', null=True, blank=True)
    DynasphereOperationIndicator=models.NullBooleanField(null=True, blank=True)
    SurgeProtectorVisual=models.ForeignKey('SubstationInspectionChoice', related_name='SurgeProtectorVisual', null=True, blank=True)
    Debris=models.ForeignKey('SubstationInspectionChoice', related_name='Debris', null=True, blank=True)
    FenceGroundLevel=models.ForeignKey('SubstationInspectionChoice', related_name='FenceGroundLevelBelow', null=True, blank=True)
    FenceOverallCondition=models.ForeignKey('SubstationInspectionChoice', related_name='FenceOverallCondition', null=True, blank=True)
    FenceSecurityWire=models.ForeignKey('SubstationInspectionChoice', related_name='FenceSecurityWire', null=True, blank=True)
    FenceWarningSign=models.ForeignKey('SubstationInspectionChoice', related_name='FenceWarningSign', null=True, blank=True)
    GateGround=models.ForeignKey('SubstationInspectionChoice', related_name='GateGround', null=True, blank=True)
    GateLock=models.ForeignKey('SubstationInspectionChoice', related_name='GateLock', null=True, blank=True)
    GateWarningSign=models.ForeignKey('SubstationInspectionChoice', related_name='GateWarningSign', null=True, blank=True)
    MaterialStore=models.ForeignKey('SubstationInspectionChoice', related_name='MaterialStore', null=True, blank=True)
    SpillPreventionPlan=models.ForeignKey('SubstationInspectionChoice', related_name='SpillPreventionPlan', null=True, blank=True)
    AMRVisual=models.ForeignKey('SubstationInspectionChoice', related_name='AMRVisual', null=True, blank=True)
    Vegetation=models.ForeignKey('SubstationInspectionChoice', related_name='Vegetation', null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField(null=True, blank=True)
    class Meta:
        db_table=u'substationinspection'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationInspection.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        return str(self.Substation.Name) + ' ' + str(self.id) + ' ' + str(self.CreateDateTime)[:10] 

class SubstationInspectionChoice(models.Model):
    Name=models.CharField(max_length=250, unique=True)
    Description=models.TextField(null=True, blank=True)
    ActiveIndicator=models.NullBooleanField(null=True, blank=True)
    ResidesOnServerIndicator=models.NullBooleanField()
    class Meta:
        db_table=u'substationinspectionchoice'
        app_label='djangoproject'
    def GetIndexWhenReverseSorted(self):
        Objects=SubstationInspectionChoice.objects.all().order_by('-id')
        i = 0
        while(True):
            if(Objects[i] == self):
                return i
            i = i + 1
    def GetCommandidData(self):
        if(self.ResidesOnServerIndicator):
            return self.id, None
        return None, self.GetIndexWhenReverseSorted()
    def __unicode__(self):
        StringList=[]
        if(not self.ActiveIndicator):
            StringList.append('Inactive ')
        StringList.append(self.Name)
        return ''.join(StringList)


